<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Listeners;

use App\Enums\AdminCheckEnum;
use App\Enums\CheckStatusEnum;
use App\Enums\MessageTypeEnum;
use App\Events\CheckSuccess;
use App\Exceptions\ApiException;
use App\Models\Message;
use Illuminate\Support\Facades\Log;

class CheckSuccessListener
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
    }

    /**
     * Handle the event.
     *
     * @param CheckSuccess $event
     */
    public function handle($event)
    {
        $checkSuccessFormat = $event->checkSuccessFormat;
        $userId = $checkSuccessFormat->getUserId();
        $checkType = $checkSuccessFormat->getCheckType();
        $checkStatus = $checkSuccessFormat->getCheckStatus();
        if ($checkStatus == CheckStatusEnum::CHECK_ADOPT) {
            $message = $this->getMessageText($checkType);
        } else {
            $message = $this->getFailMessageText($checkType);
        }
        Message::query()->create([
            'title' => $message,
            'content' => $message,
            'push_user_id' => request('user_id') ?? 1,
            'push_time' => time(),
            'user_id' => $userId,
            'type' => MessageTypeEnum::CHECK_MESSAGE_TYPE,
        ]);
    }

    private function getMessageText(int $checkType)
    {
        $messages = [
            AdminCheckEnum::CHECK_FLOW_USER => '您的人口信息审核已通过',
            AdminCheckEnum::CHECK_NOT_FLOW_USER => '您的人口信息审核已通过',
            AdminCheckEnum::CHECK_SEARCH_NOTICE => '您的寻人(物)启事审核已通过',
            AdminCheckEnum::CHECK_HOUSE => '您的房屋审核已通过',
            AdminCheckEnum::CHECK_COMPANY => '您的企业审核已通过',
            AdminCheckEnum::CHECK_PASS_CARD => '您的出入证审核已通过',
            AdminCheckEnum::CHECK_HOUSE_SCORE => '您的房屋评分审核已通过',
            AdminCheckEnum::CHECK_RECRUITMENT => '您的招聘审核已通过',
            AdminCheckEnum::CHECK_HOUSE_RENT => '您的房屋出租审核已通过',
        ];

        if (isset($messages[$checkType])) {
            return $messages[$checkType];
        }
        Log::error('审核类型异常', [
            'checkType' => $checkType,
        ]);
        throw new ApiException('审核类型异常', 1);
    }

    private function getFailMessageText(int $checkType)
    {
        $messages = [
            AdminCheckEnum::CHECK_FLOW_USER => '您的人口信息审核未通过',
            AdminCheckEnum::CHECK_NOT_FLOW_USER => '您的人口信息审核未通过',
            AdminCheckEnum::CHECK_SEARCH_NOTICE => '您的寻人(物)启事审核未通过',
            AdminCheckEnum::CHECK_HOUSE => '您的房屋审核未通过',
            AdminCheckEnum::CHECK_COMPANY => '您的企业审核未通过',
            AdminCheckEnum::CHECK_PASS_CARD => '您的出入证审核未通过',
            AdminCheckEnum::CHECK_HOUSE_SCORE => '您的房屋评分审核未通过',
            AdminCheckEnum::CHECK_RECRUITMENT => '您的招聘审核未通过',
            AdminCheckEnum::CHECK_HOUSE_RENT => '您的房屋出租审核未通过',
        ];

        if (isset($messages[$checkType])) {
            return $messages[$checkType];
        }
        Log::error('审核类型异常', [
            'checkType' => $checkType,
        ]);
        throw new ApiException('审核类型异常', 1);
    }
}
